﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTTT.DataAccess.DataInf;
using HTTT.Utilities;

namespace HTTT.DataAccess
{
    public class HT_RequestDAO : IHT_RequestDAO
    {
        /// <summary>
        /// Gets all HT_Request
        /// </summary>
        /// <returns></returns>
        /// 

        private static AppEntities dataContext;

        private IRepository<HT_Request> repository;

        public HT_RequestDAO()
        {
            dataContext = new AppEntities();
            repository = new RepositoryDAO<HT_Request>(dataContext);
        }


        public IEnumerable<HT_Request> GetAllHT_Request()
        {
            return repository.GetQuery().OrderBy(aobj => aobj.ID);

        }

        public HT_Request GetRequest_ID(Guid id)
        {
            return repository.Find(obj => obj.ID == id).SingleOrDefault();

        }

        public bool InsertRequest(HT_Request inRequest)
        {
            if (inRequest == null) return false;
            repository.Add(inRequest);
            return repository.SaveChanges() > 0;
        }

        public bool UpdateRequest(HT_Request inHT_Request)
        {
            if (inHT_Request == null) return false;
            HT_Request upHT_Request = repository.First(obj => obj.ID == inHT_Request.ID);

            if (upHT_Request == null) return false;

            upHT_Request.Action = inHT_Request.Action;
            upHT_Request.ActionDescription = inHT_Request.ActionDescription;
            upHT_Request.ActionName = inHT_Request.ActionName;
            upHT_Request.ApproveDate = inHT_Request.ApproveDate;
            upHT_Request.Approvedby = inHT_Request.Approvedby;
            upHT_Request.Note = inHT_Request.Note;
            upHT_Request.Status = inHT_Request.Status;
            upHT_Request.Requestedby = inHT_Request.Requestedby;
            upHT_Request.RequestDate = inHT_Request.RequestDate;
            return repository.SaveChanges() > 0;
        }

        public IEnumerable<HT_Request> GetHT_RequestByStatus(HT_REQUESTSTATUS hT_REQUESTSTATUS)
        {
            string requestStatus = hT_REQUESTSTATUS.ToString();
            return repository.GetQuery().Where(o => o.Status == requestStatus ).OrderBy(aobj => aobj.RequestDate);
        }
    }
}
